package com.pengpeng.server.dao;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.pengpeng.server.database.Database;
import com.pengpeng.server.database.DatabaseFactory;
import com.pengpeng.server.database.ISqlObj;
import com.pengpeng.server.entity.ClientError;
import com.pengpeng.server.entity.ServerError;

public class ClientErrorDao{

	private DatabaseFactory databasefactory;
		
	public List<ClientError> query()
	{
		RowMapper<ClientError> mapper=new RowMapper<ClientError>()
		{
			@Override
			public ClientError mapRow(ResultSet rs, int arg1) throws SQLException 
			{
				
				String errorid;
				String phonenum;
				String clienttype;
				String clientversionnum;
				String errortype;                   
				String appname;        
				String funcname;     
			    String errorcontent;  
			    String errortimestr;
				Date   errortime;
				
				errorid=rs.getString("errorid");
				phonenum=rs.getString("phonenum");
				clienttype=rs.getString("clienttype");
				clientversionnum=rs.getString("clientversionnum");
				errortype=rs.getString("errortype");                   
				appname=rs.getString("appname"); 
				funcname=rs.getString("funcname");     
			    errorcontent=rs.getString("errorcontent");       
				errortimestr=rs.getString("errortime");
		
				SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				try {
					errortime= dateformat.parse(errortimestr);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					errortime=new Date();
				}
					
				ClientError clienterror=new ClientError();
				clienterror.setErrorid(errorid);
				clienterror.setPhonenum(phonenum);
				clienterror.setClienttype(clienttype);
				clienterror.setClientversionnum(clientversionnum);
				clienterror.setErrortype(errortype);
				clienterror.setAppname(appname);
				clienterror.setFuncname(funcname);
				clienterror.setErrorcontent(errorcontent);
				clienterror.setErrortime(errortime);
				
				return clienterror;
			}//mapRow
		};//new RowMapper
		
		Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("queryClientError");
	    try {  
	    	
	    	List<ClientError> clienterrorlist=jdbctemplate.query(sqlstr, mapper);
		    return  clienterrorlist; 
	        } catch (EmptyResultDataAccessException e) {  
	          return null;  
	    } 
	}//query
	
	public List<ClientError> queryByErrortime(Date starttime,Date endtime)
	{
		RowMapper<ClientError> mapper=new RowMapper<ClientError>()
		{
			@Override
			public ClientError mapRow(ResultSet rs, int arg1) throws SQLException 
			{
				
				String errorid;
				String phonenum;
				String clienttype;
				String clientversionnum;
				String errortype;                   
				String appname;        
				String funcname;     
			    String errorcontent;  
			    String errortimestr;
				Date   errortime;
				
				errorid=rs.getString("errorid");
				phonenum=rs.getString("phonenum");
				clienttype=rs.getString("clienttype");
				clientversionnum=rs.getString("clientversionnum");
				errortype=rs.getString("errortype");                   
				appname=rs.getString("appname"); 
				funcname=rs.getString("funcname");     
			    errorcontent=rs.getString("errorcontent");       
				errortimestr=rs.getString("errortime");
		
				SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				try {
					errortime= dateformat.parse(errortimestr);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					errortime=new Date();
				}
					
				ClientError clienterror=new ClientError();
				clienterror.setErrorid(errorid);
				clienterror.setPhonenum(phonenum);
				clienterror.setClienttype(clienttype);
				clienterror.setClientversionnum(clientversionnum);
				clienterror.setErrortype(errortype);
				clienterror.setAppname(appname);
				clienterror.setFuncname(funcname);
				clienterror.setErrorcontent(errorcontent);
				clienterror.setErrortime(errortime);
				
				return clienterror;
			}//mapRow
		};//new RowMapper
		
		Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		
		
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    String starttimestr=dateformat.format(starttime);;
		String endtimestr=dateformat.format(endtime);;
		
		Object[] params = new Object[] {starttimestr,endtimestr};
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("queryClientErrorByErrortime");
	    try {  
	    	
	    	List<ClientError> clienterrorlist=jdbctemplate.query(sqlstr,params, mapper);
		    return  clienterrorlist; 
	        } catch (EmptyResultDataAccessException e) {  
	          return null;  
	    } 
	}//queryByErrortime
	
	public boolean update(ClientError clienterror)
	{
		Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		
		String errorid=clienterror.getErrorid();
		String phonenum=clienterror.getPhonenum();
		String clienttype=clienterror.getClienttype();
		String clientversionnum=clienterror.getClientversionnum();
		String errortype=clienterror.getErrortype();                   
		String appname=clienterror.getAppname();        
		String funcname=clienterror.getFuncname();     
	    String errorcontent=clienterror.getErrorcontent();       
		Date   errortime=clienterror.getErrortime();
		
	    
	    SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    String errortimestr=dateformat.format(errortime);
			
		Object[] params = new Object[] {errorid,phonenum,clienttype,clientversionnum,errortype,appname,funcname,errorcontent,errortimestr,errorid};
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("updateClientError");
		jdbctemplate.update(sqlstr,params);		
		return true;
	}
	
	public boolean insert(ClientError clienterror)
	{
		Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		
		String errorid=clienterror.getErrorid();
		String phonenum=clienterror.getPhonenum();
		String clienttype=clienterror.getClienttype();
		String clientversionnum=clienterror.getClientversionnum();
		String errortype=clienterror.getErrortype();                   
		String appname=clienterror.getAppname();        
		String funcname=clienterror.getFuncname();     
	    String errorcontent=clienterror.getErrorcontent();       
		Date   errortime=clienterror.getErrortime();
	    
	   
	    SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    String errortimestr=dateformat.format(errortime);
			
	    if(errorid.equals(""))
	    {
	    	dateformat = new SimpleDateFormat("yyyyMMddHHmmssms");
	    	errorid=phonenum+errortype+dateformat.format(new Date());
	    }
	    
	    Object[] params = new Object[] {errorid,phonenum,clienttype,clientversionnum,errortype,appname,funcname,errorcontent,errortimestr};
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("insertClientError");
		jdbctemplate.update(sqlstr,params);		
		return true;
	}
	public boolean delete(ServerError servererror)
	{
		Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		Object[] params = new Object[] {servererror.getErrorid()};
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("deleteClientError");
		jdbctemplate.update(sqlstr,params);		
		return true;
	}//delete
	public DatabaseFactory getDatabasefactory() {
		return databasefactory;
	}
	public void setDatabasefactory(DatabaseFactory databasefactory) {
		this.databasefactory = databasefactory;
	}

}
